/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.absisgroup.nsc.home;

import com.absisgroup.nsc.users.UsersEntity;
import com.absisgroup.nsc.utilities.GenericServlet;
import com.absisgroup.nsc.utilities.PermissionControl;
import com.absisgroup.nsc.utilities.Util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 *
 * @author Hue
 */
public class GetQnodes extends GenericServlet {

    @Override
    public int process(final Connection con, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
        final String node = request.getParameter("node");
        if(node == null || "root".equals(node)) {
            writeToResponse(response, loadCategories(con));
        }
        else if(node.indexOf("CAT:") != -1) {
            final UsersEntity user = getCurrentUser(request);
            int adminOption = 0;
            if(PermissionControl.hasPermission(con, user.getUsrPk().longValue(), getModPK(), "NSC_ISADMIN")) {
                adminOption = 1;
            }
            final String ncatPk = Util.replace(node, "CAT:", "");
            writeToResponse(response, loadQueries(con, ncatPk, adminOption));
        }
        
        return DONT_RUN_JSP;
    }
    
    private String loadCategories(final Connection con) throws Exception {
        final JSONArray jsArray = new JSONArray();
        final String strQuery = "select NCAT_PK, NCAT_NAME from nsc_category " + 
                                "Order by ncat_name";
        final PreparedStatement pStmt = con.prepareStatement(strQuery);
        final ResultSet rs = pStmt.executeQuery();
        while(rs.next()) {
            final JSONObject jsObj = new JSONObject();
            jsObj.put("id", "CAT:" + rs.getString("NCAT_PK"));
            jsObj.put("text", rs.getString("NCAT_NAME"));
            jsArray.put(jsObj);
        }
        pStmt.close();
        
        return jsArray.toString();
    }
    
    private String loadQueries(final Connection con, final String ncatPk, final int adminOption) throws Exception {
        final JSONArray jsArray = new JSONArray();
        final String strQuery = "select NQUE_PK, NQUE_NAME, NQUE_DESCRIPTION from nsc_query " + 
                                "where NQUE_NCAT_FK = ? And nque_isadmin <= ? " +
                                "Order by nque_name";
        final PreparedStatement pStmt = con.prepareStatement(strQuery);
        pStmt.setString(1, ncatPk);
        pStmt.setInt(2, adminOption);
        final ResultSet rs = pStmt.executeQuery();
        while(rs.next()) {
            final JSONObject jsObj = new JSONObject();
            jsObj.put("id", rs.getString("NQUE_PK"));
            jsObj.put("text", rs.getString("NQUE_NAME"));
            jsObj.put("qtip", rs.getString("NQUE_DESCRIPTION"));
            jsObj.put("leaf", true);
            jsArray.put(jsObj);
        }
        pStmt.close();
        
        return jsArray.toString();
    }
}
